﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Data;
using INTEX.SCHEDULER;
using WPFExcelReport.Helpers;
using System;
using ReflectionStudio.Core.Events;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TMRILI
    {
        #region ==IsWD===========
        public static readonly DependencyProperty IsWDProperty = DependencyProperty.Register("IsWD",
            typeof(bool), typeof(TMRILI),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWDChanged));
        public bool IsWD
        {
            get
            {
                return (bool)GetValue(IsWDProperty);
            }
            set
            {
                SetValue(IsWDProperty, value);
            }
        }
        private static void OnIsWDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMRILI;
            if (src != null)
            {
                src.IsWD = (bool)e.NewValue; 
            }
        }

        #endregion
        #region ==dwAM===========
        public static readonly DependencyProperty dwAMProperty = DependencyProperty.Register("dwAM",
            typeof(DataRow), typeof(TMRILI),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwAMChanged));
        public DataRow dwAM
        {
            get
            {
                return (DataRow)GetValue(dwAMProperty);
            }
            set
            {
                SetValue(dwAMProperty, value);
                OnMachineChg(value);
            }
        }
        private static void OndwAMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMRILI;
            if (src != null)
            {
                src.dwAM = (DataRow)e.NewValue;

            }
        }

        public virtual void OnMachineChg(DataRow dwAM)
        {
            dwData = TBMain.NewRow();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["MACHINEID"] = " AND MACHINEID LIKE '" + dwAM["MACHINEID"].ToString() + "%'";
            SResult = TMV.QTMKRF12(dwData);
            MergeData(SResult, true, true);
        }
        #endregion
        #region ==dwTAM===========
        public static readonly DependencyProperty dwTAMProperty = DependencyProperty.Register("dwTAM",
            typeof(DataRow), typeof(TMRILI),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwTAMChanged));
        public DataRow dwTAM
        {
            get
            {
                return (DataRow)GetValue(dwTAMProperty);
            }
            set
            {
                SetValue(dwTAMProperty, value);
             }
        }
        private static void OndwTAMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMRILI;
            if (src != null)
            {
                src.dwTAM = (DataRow)e.NewValue;

            } 
        }
 
        #endregion
 
         
        public TMRILI()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_MKRF();
            DataContext = MV;
            TMV.Scheduler = new ITXSSPA();
            InitializeComponent();
            TMV.SetUpAV33();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            SResult = TMV.QTMKRF00(null);
            MergeData(SResult, false);
            TBMain.Clear();
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            QTY=TMV.ABFAMASCHZT;
        }
        private void btnRefreshMachine_Click(object sender, RoutedEventArgs e)
        {
            Scheduler.PrepareAllJob(false);

        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TMV.TBMain.NewRow();
            dwData["ABFKSTKRS"] =KSTKRS;
            dwData["ABFKST"] =KST; 
            dwData["ABFMTYP"] =AMTYP;
            dwData["JOBTYPE"] = "X";
            SResult = TMV.QTMKRF13(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            foreach(DataRow row in TBMain.Rows)
            {
                TMV.UpdateICC_MKRF(row, 1);
            } 
            base.SaveCommand(sender, e);
        }
      
        public override void OnQTYChg(double value)
        {
          
            base.OnQTYChg(value);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {


            if (!IsNew)
            {
                IsNew = true;
                return;

            }
            if (MACHINEID == "")
            {
                MessageBox.Show("请选择设备");
                return;
            }
            try
            {

                if (DTSTART.Trim().Length != 8)
                {
                    MessageBox.Show("请设定正确的日期");
                    return;
                }
                filter = "ABFAUFKRS='RL' AND ABFAUFLFD='" + DTSTART.Substring(0, 6) + "' AND ABFAUFPOS='" + Helper.Right("000" + DTSTART.Substring(6, 2), 3) + "' AND MKFTP='0000'";
                rows = TBMain.Select(filter);
                if (rows.Length == 0)
                {
                    dwData = TBMain.NewRow();
                    TMV.SetUpNewRow(ref dwData);
                    dwData["ABAID"] = "RL" + INTEXDBC.GetSeq();
                    dwData["ABFAUFKRS"] = "RL";
                    dwData["ABFAUFLFD"] = DTSTART.Substring(0, 6);
                    dwData["ABFAUFPOS"] = Helper.Right("000" + DTSTART.Substring(6, 2), 3); 
                    dwData["ABFBNRKRS"] = MACHINEID.Substring(0,2);
                    dwData["ABFBLFDNR"] = MACHINEID.Substring(2, 4);
                    dwData["ABFBPOS"] = MACHINEID.Substring(6, 2);
                    dwData["ABFMTYP"] = MACHINEID.Substring(8,8);
                    dwData["ABFKSTKRS"] = MACHINEID.Substring(24, 2);
                    dwData["ABFKST"] = MACHINEID.Substring(26, 4);
                    dwData["MACHINEID"] = "m" + MACHINEID.Substring(0, 2).Trim()+"_"+ MACHINEID.Substring(2, 4)+"_"+MACHINEID.Substring(6, 2);
                    dwData["ABPART"] = Helper.ConvertGB2312To8859P1("请定义");
                    dwData["MKFTP"] = "0000";
                    dwData["MKFPLDATV"] = DTSTART;
                    dwData["MKFPLTIME"] = 0;
                    dwData["ABFAMASCHZT"] = 0;
                    dwData["JP50"] = "0";
                    dwData["ABFFIX"] = "J";
                    dwData["JOBTYPE"] = "X";
                    dwData["ABFISTARTDAT"] = "0"; 
                    dwData["MKFTP"] = 0;
                }
                else
                {
                    dwData = rows[0];
                    dwData["ABAID"] = "RL"+INTEXDBC.GetSeq();
                    int NNUM = Convert.ToInt16(dwData["JP50"].ToString());
                    NNUM = NNUM + 1;
                    dwData["JP50"] = NNUM;
                    dwData["MKFTP"] = Helper.Right("0000" + NNUM, 4);
                }  
                TMV.INTMKRF00(dwData);
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                Scheduler.PrepareAllJob(false);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            IsNew = false;
            base.NewCommand(sender, e);
        }
         
        public   void WDCommand(object sender, RoutedEventArgs e)
        {
            if (MACHINEID == "")
            {
                MessageBox.Show("请先选择设备");
                return;
            }
            if (!IsWD)
            {
                TMV.NEWWD(MACHINEID);
                IsWD = true;

                vwMaster.View.MoveCurrentToFirst();
                return;
            }

            IsWD = false;     
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DMKRF01(dwMaster);
            filter = "ABAID='" + dwMaster["ABAID"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach(DataRow row in rows)
            {
                row.Delete();
            } 
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
        public override void OnDTSTARTChg(string value)
        {
            if (dwMaster == null)
                return;
            TMV.MKFPLDATV = DTSTART;
            base.OnDTSTARTChg(value);
        }
        private void btnRLToAll_Click(object sender, RoutedEventArgs e)
        {
            if (dwMKRF == null)
            {
                MessageBox.Show("请选择事件");
                return;
            }
            try
            {

                filter = "ABFAUFKRS='RL' AND ABFAUFLFD='" + dwMKRF["ABFAUFLFD"].ToString() + "' AND ABFAUFPOS='" + dwMKRF["ABFAUFPOS"].ToString() + "'";
                rows = TBMain.Select(filter);
                DataRow dwNMRKF = null;
                DataRow dwXMRKF = null;
                int NNUM = 0;
                string MachineID = "";

                dwXMRKF = rows[0];
                NNUM = Convert.ToInt16(dwXMRKF["JP50"].ToString());
                MachineID = dwXMRKF["MACHINEID"].ToString();
                var qM = from DataRow row in MV.SetProd.Tables["AMSTAMM"].AsEnumerable()
                         where row["MACHINEID"].ToString() != MachineID
                         select row;
                foreach (DataRow dwMachine in qM)
                {
                    NNUM = NNUM + 1;
                    dwNMRKF = TBMain.NewRow();
                    dwNMRKF.ItemArray = dwXMRKF.ItemArray;// INTEXDBC.Convert2ICC_MKRF(dwXMRKF, "YGR", true).ItemArray;
                    dwNMRKF["ABAID"] = INTEXDBC.GetSeq();
                    dwNMRKF["ABFBNRKRS"] = dwMachine["AMNRKRS"];
                    dwNMRKF["ABFBLFDNR"] = dwMachine["AMLFDNR"];
                    dwNMRKF["ABFBPOS"] = dwMachine["AMPOS"];
                    dwNMRKF["MACHINEID"] = dwMachine["MACHINEID"];
                    dwNMRKF["MKFTP"] = Helper.Right("0000" + NNUM, 4);
                    dwNMRKF["ABPART"] = dwXMRKF["ABPART"];
                    dwNMRKF["MKFPLDATV"] = dwXMRKF["MKFPLDATV"];
                    dwNMRKF["MKFPLTIME"] = dwXMRKF["MKFPLTIME"];
                    dwNMRKF["ABFAMASCHZT"] = dwXMRKF["ABFAMASCHZT"];
                    TMV.InsertICC_MKRF(dwData, 0);
                    TBMain.Rows.InsertAt(dwData, 0);
                }

                dwXMRKF["JP50"] = NNUM;
                TMV.UpdateICC_MKRF(dwXMRKF);
                Scheduler.PrepareAllJob(false);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {

            if (!IsCopy)
            {
                IsCopy = true;
                dwTAM = null;
                cAM02.vwMaster.View.MoveCurrentTo(-1);
                return;

            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择基础记录");
                return;
            }
            if (dwTAM == null)
            {
                MessageBox.Show("请选择目标设备");
                return;
            }

            try
            { 
                int NNUM = Convert.ToInt16(dwMaster["JP50"].ToString()); 
                NNUM = NNUM + 1;
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;// INTEXDBC.Convert2ICC_MKRF(dwXMRKF, "YGR", true).ItemArray;
                dwData["ABAID"] = INTEXDBC.GetSeq();
                dwData["ABFBNRKRS"] = dwTAM["AMNRKRS"];
                dwData["ABFBLFDNR"] = dwTAM["AMLFDNR"];
                dwData["ABFBPOS"] = dwTAM["AMPOS"];
                dwData["MACHINEID"] = dwTAM["MACHINEID"];
                dwData["MKFTP"] = Helper.Right("0000" + NNUM, 4);
                dwData["ABPART"] = dwMaster["ABPART"];
                dwData["MKFPLDATV"] = dwMaster["MKFPLDATV"];
                dwData["MKFPLTIME"] = dwMaster["MKFPLTIME"];
                dwData["ABFAMASCHZT"] = dwMaster["ABFAMASCHZT"];
                TMV.InsertICC_MKRF(dwData, 0);
                TBMain.Rows.InsertAt(dwData, 0); 
                dwMaster["JP50"] = NNUM;
                TMV.UpdateICC_MKRF(dwMaster);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            IsCopy = false;
            dwTAM = null;
            cAM02.vwMaster.View.MoveCurrentTo(-1);
            base.CopyCommand(sender, e);
        }

        private void QTY_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            if (dwMaster == null)
                return;
            DateTime JobStart = new DateTime();
            DateTime JobEnd = new DateTime();
            TMV.ABFAMASCHZT = QTY;
            Helper.GetDateTime(ref JobStart, TMV.MKFPLDATV, TMV.MKFPLTIME);
         
            JobEnd = JobStart.AddMinutes(TMV.ABFAMASCHZT);
            string jdstart = "", jtstart = "";
            Helper.GetStdDate(ref jdstart, ref jtstart, JobEnd);
            dwMaster["ENDATE"] = jdstart;
            dwMaster["ENDTIME"] = jtstart;
            dwMaster["SFILETME"] = JobStart.ToFileTime();
            dwMaster["EFILETME"] = JobEnd.ToFileTime();
            TMV.UpdateICC_MKRF(dwMaster, 1); 
        }

        private void QueryWDCommand(object sender, RoutedEventArgs e)
        {
            if (MACHINEID == "")
                return;
            dwData = TMV.TBMain.NewRow();
            dwData["ABFBNRKRS"] = MACHINEID.Substring(0, 2);
            dwData["ABFBLFDNR"] = MACHINEID.Substring(2, 4);
            dwData["ABFBPOS"] = MACHINEID.Substring(6, 2);
            dwData["JOBTYPE"] = "W";
            SResult = TMV.QTMKRF15(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
    }
}
